<?php
/**
 *	Views of Home Page.
 *	@package		tools.BugTracker
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Views of Home Page.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Core_View
 *	@uses			Database_StatementBuilder
 *	@uses			BugStatements
 *	@uses			User
 *	@uses			Message
 *	@uses			Search
 *	@uses			BugViews
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 *	@todo			Language Support of Messages
 *	@todo			Code Documentation
 */
class View_Bug_Dashboard extends CMF_Krypton_Core_View
{
	/**
	 *	...
	 *	@access		public
	 *	@return		string
	 */
	public function buildField()
	{
		$config	= $this->registry->get( 'config' );
		$auth	= $this->registry->get( 'auth' );
		$words	= $this->words['main']['home'];

		$ui['words']	= $words;

		$user			= new User( $auth->getCurrentUser() );
		$ui['userdata']	= $user->getData( TRUE );

		$ui['latest_own']		= "";
		$ui['latest_assigned']	= "";
	
		$ui['graph_work']		= $this->buildGraph( "work" );
		$ui['graph_priority']	= $this->buildGraph( "priority" );
		$ui['graph_status']		= $this->buildGraph( "status" );
		$ui['graph_type']		= $this->buildGraph( "type" );
		$ui['latest_new']		= $this->buildLatestNew();
		$ui['latest_fixed']		= $this->buildLatestFixed();
		$ui['latest_messages']	= $this->buildLatestMessages();
		$ui['searchs']			= $this->buildSearchs();
		$ui['languages']		= $this->buildLanguages();
		$ui['themes']			= $this->buildThemes();
		if( $user->hasRole( $auth->getCurrentUser(), 4 ) )		//  is a Reporter
			$ui['latest_own']		= $this->buildLatestOwn();
		if( $user->hasRole( $auth->getCurrentUser(), 3 ) )		//  is a Developer
			$ui['latest_assigned']	= $this->buildLatestAssigned();
		return $this->loadTemplate( "home.home", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildGraph( $type )
	{
		$config	= $this->registry->get( 'config' );

		$timestamp	= 0;
		$bug		= new Bug();
		$data		= array_pop( $bug->getAllData( array( 'created' ), array(), array( 'created' => "DESC" ) ) );
		if( $data )
			$timestamp	= $data[0]['created'];
		
		$cacheFile	= $config['paths']['cache']."graph_".$type;
		if( !( file_exists( $cacheFile ) &&  $timestamp <= filemtime( $cacheFile ) ) )
		{
			$ui['config']	= $this->registry->get( 'config' );
			$ui['type']		= $type;
			return $this->loadTemplate( "home.buildGraph", $ui );
		}
		else
		{
			$ui['image']	= file_get_contents( $cacheFile );
			$ui['type']		= $type;
			return $this->loadTemplate( "home.loadGraph", $ui );	
		}
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildLatestNew()
	{
		$dbc	= $this->registry->get( 'dbc' );
		$config	= $this->registry->get( 'config' );
		$words	= $this->words['main']['home'];

		$bugview	= new BugViews();
		$latestNew	= array();
		$sb	= new Database_StatementBuilder( $config['config']['table_prefix'] );
		$bs	= new BugStatements( $sb );
		$bs->orderBy( "created", "DESC" );
		$bs->collectBugs();
		$bs->setLimit( 5 );
		$bs->withStates( array( 0 ) );
		$query	= $sb->buildStatement();
		$result	= $dbc->execute( $query );
		$latest	= array();
		while( $bug	= $result->fetchNextArray( false ) )
		{
			$icon_url		= $bugview->getIconURL( $bug['priority'], $bug['status'] );
			$title			= $this->str_shorten( $bug['title'], $config['layout']['shorten_field'] );
			$link			= $this->html->Link( "?link=bug&bug_id=".$bug['bug_id'], $title );
			$latestNew[]	= "<li class='bug' style='background-image: url(".$icon_url.");'>".$link."</li>";
		}
		$ui['heading']	= $words['heading_latest_new'];
		$ui['list']		= implode( "\n  ", $latestNew );
		return $this->loadTemplate( "home.latestNew", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildLatestFixed()
	{
		$dbc	= $this->registry->get( 'dbc' );
		$config	= $this->registry->get( 'config' );
		$words	= $this->words['main']['home'];

		$bugview	= new BugViews();
		$latestFixes	= array();
		$sb	= new Database_StatementBuilder( $config['config']['table_prefix'] );
		$bs	= new BugStatements( $sb );
		$bs->collectBugs();
		$bs->setLimit( 5 );
		$bs->withStates( array( 2 ) );
		$bs->orderBy( "modified", "DESC" );
		$query	= $sb->buildStatement();
		$result	= $dbc->execute( $query );
		$latest	= array();
		while( $bug	= $result->fetchNextArray( false ) )
		{
			$icon_url		= $bugview->getIconURL( $bug['priority'], $bug['status'] );
			$title			= $this->str_shorten( $bug['title'], $config['layout']['shorten_field'] );
			$link			= $this->html->Link( "?link=bug&bug_id=".$bug['bug_id'], $title );
			$latestFixes[]	= "<li class='bug' style='background-image: url(".$icon_url.");'>".$link."</li>";
		}
		$ui['heading']	= $words['heading_latest_fixed'];
		$ui['list']		= implode( "\n  ", $latestFixes );
		return $this->loadTemplate( "home.latestFixes", $ui );
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildSearchs()
	{
		$auth	= $this->registry->get( 'auth' );
		$words	= $this->words['main']['home'];
		
		$search	= new Search();
		$search->focusForeign( "user_id", $auth->getCurrentUser() );
		$data	= $search->getData( false );
		if( count( $data ) )
		{
			$list	= array();
			foreach( $data as $entry )
			{
				$link	= $this->html->Link( "?link=bug&search_id=".$entry['search_id']."&searchBugs=1", $entry['title'] );
				$list[]	= $this->html->ListItem( $link );
			}
			$list		= implode( "\n\t", $list );
			$ui['list']		= $list;
			$ui['heading']	= $words['heading_searchs'];
			return $this->loadTemplate( "home.searchs", $ui );
		}
		return "";
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildLatestMessages()
	{
		$config	= $this->registry->get( 'config' );
		$auth	= $this->registry->get( 'auth' );
		$words	= $this->words['main']['home'];

		$message	= new Message();
		$message->focusForeign( "target", $auth->getCurrentUser() );
		$message->focusForeign( "status", 0 );
		$list	= $message->getData( false );
		if( count( $list ) )
		{
			$latestMessages	= array();
			foreach( $list as $message )
			{
				$image_url	= $config['paths']['themes'].$config['layout']['theme']."/icons/message".$message['status'].".png";
			//	$status	= $this->html->Image( $config['paths']['themes'].$config['layout']['theme']."/icons/message".$message['status'].".png", $this->words['message_states'][$message['status']] );
			//	$latest_messages[]	= "<li>".$status.$this->html->Link( "?link=messages&message_id=".$message['message_id'], $this->str_shorten( $message['subject'], 100 ) )."</li>";
				$latestMessages[]	= "<li style='background: url(".$image_url.") 1px 0px no-repeat; padding-left: 18px'>".$this->html->Link( "?link=message&message_id=".$message['message_id'], $this->str_shorten( $message['subject'], 100 ) )."</li>";
			}
			$ui['heading']	= $words['heading_latest_messages'];
			$ui['list']		= implode( "\n  ", $latestMessages );
			return $this->loadTemplate( "home.latestMessages", $ui );
		}
		return "";
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildLatestOwn()
	{
		$dbc	= $this->registry->get( 'dbc' );
		$config	= $this->registry->get( 'config' );
		$auth	= $this->registry->get( 'auth' );
		$words	= $this->words['main']['home'];

		$bugview	= new BugViews();
		$latestOwn	= array();
		$sb	= new Database_StatementBuilder( $config['config']['table_prefix'] );
		$bs	= new BugStatements( $sb );
		$bs->collectBugs();
		$bs->setLimit( 5 );
		$bs->withAuthor( $auth->getCurrentUser() );
		$bs->orderBy( "modified", "DESC" );
		$query	= $sb->buildStatement();
		$result	= $dbc->execute( $query );
		if( $result->recordCount() )
		{
			$latest	= array();
			while( $bug	= $result->fetchNextArray( false ) )
			{
				$icon_url		= $bugview->getIconURL( $bug['priority'], $bug['status'] );
				$title		= $this->str_shorten( $bug['title'], $config['layout']['shorten_field'] );
				$link			= $this->html->Link( "?link=bug&bug_id=".$bug['bug_id'], $title );
				$latestOwn[]	= "<li class='bug' style='background-image: url(".$icon_url.");'>".$link."</li>";
			}
			$ui['heading']	= $words['heading_latest_own'];
			$ui['list']		= implode( "\n  ", $latestOwn );
			return $this->loadTemplate( "home.latestOwn", $ui );
		}
		return "";
	}

	/**
	 *	...
	 *	@access		private
	 *	@return		string
	 */
	private function buildLatestAssigned()
	{
		$dbc	= $this->registry->get( 'dbc' );
		$config	= $this->registry->get( 'config' );
		$auth	= $this->registry->get( 'auth' );
		$words	= $this->words['main']['home'];

		$bugview	= new BugViews();
		$lan		= $this->registry->get( 'language' );
		$latestAssigned	= array();
		$sb	= new Database_StatementBuilder( $config['config']['table_prefix'] );
		$bs	= new BugStatements( $sb );
		$bs->collectBugs();
		$bs->setLimit( 5 );
		$bs->withStates( array( 1, 3 ) );
		$bs->withDeveloper( $auth->getCurrentUser() );
		$bs->orderBy( 'modified', "DESC" );
		$query	= $sb->buildStatement();
		$result	= $dbc->execute( $query );
		if( $result->RecordCount() )
		{
			$latest	= array();
			while( $bug	= $result->fetchNextArray( false ) )
			{
				$icon_url		= $bugview->getIconURL( $bug['priority'], $bug['status'] );
				$title		= $this->str_shorten( $bug['title'], $config['layout']['shorten_field'] );
				$link			= $this->html->Link( "?link=bug&bug_id=".$bug['bug_id'], $title );
				$latestAssigned[]	= "<li class='bug' style='background-image: url(".$icon_url.");'>".$link."</li>";
			}
			$ui['heading']	= $words['heading_latest_assigned'];
			$ui['list']		= implode( "\n  ", $latestAssigned );
			return $this->loadTemplate( "home.latestAssignments", $ui );
		}
		return "";
	}

	/**
	 *	Builds List of available Languages.
	 *	@access		private
	 *	@return		string
	 */
	private function buildLanguages()
	{
		$session	= $this->registry->get( 'session' );
		$words	= $this->words['main']['home'];

		$list		= array();
		$current	= $session->get( 'language' );
		foreach( $this->words['main']['languages'] as $language => $name )
		{
			if( $language	!= $current )
				$list[]	= $this->html->Link( "?switchLanguageTo=".$language, $name );
		}
		$ui['heading']	= $words['heading_languages'];
		$ui['list']		= implode( "<br/>", $list );
		return $this->loadTemplate( "home.languages", $ui );
	}

	/**
	 *	Builds List of available Themes.
	 *	@access		private
	 *	@return		string
	 */
	private function buildThemes()
	{
		$config		= $this->registry->get( 'config' );
		$session	= $this->registry->get( 'session' );
		$words	= $this->words['main']['home'];

		$list		= array();
		$current	= $session->get( 'theme' );
		foreach( new DirectoryIterator( $config['paths']['themes'] ) as $theme )
		{
			if( $theme->isDir() && !$theme->isDot() && $theme != $current )
				$list[]	= $this->html->Link( "?theme=".$theme, $theme );
		}
		$ui['heading']	= $words['heading_themes'];
		$ui['list']		= implode( "<br/>", $list );
		return $this->loadTemplate( "home.themes", $ui );
	}
}
?>